﻿using System;
using System.Collections;

namespace CSharp进阶_Hashtable
{
	class Test { }

	class Monster
	{
		public int id_;
		public string name_;
		public int hp_;
		public Monster(int id, string name, int hp = 10)
		{
			id_ = id;
			name_ = name;
			hp_ = hp;
			Console.WriteLine("怪物{0}已被创建", id_);
		}
		public void Dead()
		{
			Console.WriteLine("Monster: " + name_ + "(" + id_ + ") is dead!");
		}
		public override string ToString()
		{
			return "Monster: " + name_ + "(" + id_ + ") HP: " + hp_;
		}
	}
	class MonsterManager
	{
		private static MonsterManager mm = new MonsterManager();
		private static int id_ = 0;
		public Hashtable monsters_ = new Hashtable();
		private MonsterManager() {}
		public static MonsterManager Instance
		{
			get
			{
				return mm;
			}
		}
		public void AddMonster(string name)
		{
			Monster monster = new Monster(id_, name);
			monsters_.Add(monster.id_, monster);
			id_++;
		}
		public void RemoveMonster(int id)
		{
			if (monsters_.ContainsKey(id))
			{
				(monsters_[id] as Monster).Dead();
				monsters_.Remove(id);
			}
		}
	}
	class Program
	{
		static void Main(string[] args)
		{
			MonsterManager mm = MonsterManager.Instance;
			mm.AddMonster("m1");
			mm.AddMonster("m2");
			foreach (DictionaryEntry entry in mm.monsters_)
			{
				Console.WriteLine("key: {0}, value: {1}", entry.Key, entry.Value);
			}
			mm.RemoveMonster(0);
			mm.RemoveMonster(1);
			////Hashtable需要引入命名空间 System.Collections;
			//#region 定义初始化
			//Hashtable ht = new Hashtable();
			//#endregion
			//#region 增
			//ht.Add(1, "3.14");
			//ht.Add(2, "Hello");
			//ht.Add("weihe", "world");
			//ht.Add(3.14, "pai");
			//Test t = new Test();
			//ht.Add(t, "Test");
			//#endregion
			//#region 删-通过key
			//ht.Remove(1);
			//ht.Remove(1);// 删除不存在的键值对，无事发生
			//#endregion
			//#region 查
			//// 1.通过key查找
			//if (ht.Contains(2))
			//{
			//	Console.WriteLine("存在: <{0}, {1}>", 2, ht[2]);
			//}
			//else
			//{
			//	Console.WriteLine("不存在 key: 2");
			//}
			//if (ht.ContainsKey(1))
			//{
			//	Console.WriteLine("存在: <{0}, {1}>", 1, ht[1]);
			//}
			//else
			//{
			//	Console.WriteLine("不存在 key: 1");
			//}
			//// 1.通过value查找
			//if (ht.ContainsValue("Hello"))
			//{
			//	Console.WriteLine("存在: <{0}, {1}>", 2, ht[2]);
			//}
			//else
			//{
			//	Console.WriteLine("不存在 value: Hello");
			//}
			//#endregion
			//#region 改
			//ht[2] = "修改了， 是string";
			//#endregion
			//#region 遍历
			//// 1.遍历键
			//foreach (object key in ht.Keys)
			//{
			//	Console.Write("key: {0}", key);
			//	Console.WriteLine(" value: {0}", ht[key]);
			//}
			//// 2.遍历值
			//foreach (object value in ht.Values)
			//{
			//	Console.WriteLine("value: {0}", value);
			//}
			//// 3.遍历键值对
			//foreach (DictionaryEntry entry in ht)
			//{
			//	Console.Write("key: {0}", entry.Key);
			//	Console.WriteLine(" value: {0}", entry.Value);
			//}
			//// 4.迭代器遍历
			//IDictionaryEnumerator ide = ht.GetEnumerator();

			//while (ide.MoveNext())
			//{
			//	 Console.Write("key: {0}", ide.Key);
			//	Console.WriteLine(" value: {0}", ide.Value);
			//}
			//#endregion
			//#region 装箱拆箱
			//int key = 1, value = 100;
			//ht.Add(key, value);
			//value = (int)ht[key];
			//#endregion
		}
	}
}
